package com.easygem.front.service.imp;

import com.easygem.business.service.base.AbstractServiceImpl;
import com.easygem.common.error.SystemCodeEnum;
import com.easygem.common.error.SystemException;
import com.easygem.common.model.front.Source;
import com.easygem.common.vo.front.SourceVo;
import com.easygem.front.converter.SourceConverter;
import com.easygem.front.mapper.SourceMapper;
import com.easygem.front.service.SourceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

/**
 * @author 肖智
 * @version 1.0
 * @Description TODO
 * @date 2024/12/30
 */
@Service
public class SourceServiceImpl extends AbstractServiceImpl<SourceMapper, Source, Integer> implements SourceService {
    @Autowired
    private SourceMapper sourceMapper;

    @Override
    public List<Source> selectSource(String accountName) {
        //获得到登录后的用户信息，通用方式
        return sourceMapper.selectSource(accountName);
    }

    /**
     * 添加来源
     *
     * @param sourceVo 客户来源
     */
    @Override
    public void add(SourceVo sourceVo) {
        sourceVo.setCreateTime(String.valueOf(new Timestamp(System.currentTimeMillis())));
        Source source = new Source();
        //获得到登录后的用户信息，通用方式
        BeanUtils.copyProperties(sourceVo,source);
        sourceMapper.insert(source);
    }

    /**
     * 得到所有来源的所有信息
     *
     * @return 来源集合列表
     */
    @Override
    public List<SourceVo> getSourceAll(String accountName) {
        return SourceConverter.converterToSourceVoList(sourceMapper.getSourceList(accountName));
    }

    /**
     * 查询所有已启用的
     *
     * @return 用于下拉列表框
     */
    @Override
    public List<SourceVo> getSourceListByEnable(String accountName) {
        return SourceConverter.converterToSourceVoList(sourceMapper.selectSource(accountName));
    }

    /**
     * 拿到需要编辑来源的信息
     *
     * @param id id
     * @return
     */
    @Override
    public SourceVo edit(Integer id) throws SystemException {
        Source source = sourceMapper.selectByPrimaryKey(id);
        if (source == null) {
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR, "编辑的来源不存在");
        }
        return SourceConverter.converterToSourceVo(source);
    }

    /**
     * 更新来源
     *
     * @param sourceVo 来源对象
     */
    @Override
    public void update(Integer id,SourceVo sourceVo) throws SystemException {
        Source source1 = sourceMapper.selectByPrimaryKey(id);
        if(source1==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"编辑的来源不存在");
        }
        Source source = new Source();
        BeanUtils.copyProperties(sourceVo,source);
        source.setSourceId(id);
        sourceMapper.updateByPrimaryKeySelective(source);
    }
}
